using System;
using System.Collections.Generic;

public class FSM<T>
{
    static FSM()
    {
        if(!typeof(T).IsEnum)
        {
            throw new ArgumentException("T must be anenumerated type!");
        }
    }
    private T currentType;
    private IState<T> currentState;
    private Dictionary<T, IState<T>> states  = new Dictionary<T, IState<T>>();

    public FSM(Dictionary<T,IState<T>> states,T initType)
    {
        this.states = states;
        currentType = initType;
        TransitionState(initType);
    }
    public void OnUpdate()
    {
        currentState.OnUpdate();
    }
    public void TransitionState(T toType)
    {
        if(toType != null)
        {
            if(currentState != null)
            {
                currentState.OnExit(toType);
            }
            currentState = states[toType];
            currentState.OnEnter(currentType);
            currentType = toType;
        }
    }
    public T GetCurType()
    {
        return currentType;
    }
}
